﻿using System;
using System.Collections.Generic;
using System.Data;
using DAL;
using Entities;

namespace BLL
{
    public class BLL_User
    {
        private DAL_User dal_User = new DAL_User();

        public DataTable Get_User_All()
        {
            return dal_User.Get_User_All();
        }

        public DataTable Get_User(Int64 pID)
        {
            return dal_User.Get_User(pID);
        }

        public DataTable Get_User_Dynamic(string pCondition)
        {
            return dal_User.Get_User_Dynamic(pCondition);
        }

        public List<User> Lst_User(DataTable pdt)
        {
            List<User> lstReturn = new List<User>();
            if (pdt != null && pdt.Rows.Count > 0)
            {
                foreach (DataRow dr in pdt.Rows)
                {
                    User user = new User();
                    user.ID = (dr["ID"] == DBNull.Value ? 0 : Convert.ToInt32(dr["ID"]));
                    user.USERNAME = Convert.ToString(dr["USERNAME"]);
                    user.PASSWORD = Convert.ToString(dr["PASSWORD"]);
                    user.FULLNAME = Convert.ToString(dr["FULLNAME"]);
                    user.EMAIL = Convert.ToString(dr["EMAIL"]);
                    user.MOBILE = Convert.ToString(dr["MOBILE"]);
                    user.SKYPE = Convert.ToString(dr["SKYPE"]);
                    lstReturn.Add(user);
                }
            }
            return lstReturn;
        }

        public bool InsertUpdate_User(User pUser)
        {
            return dal_User.InsertUpdate_User(pUser);
        }

        public Int64 Get_User_MaxID()
        {
            return dal_User.Get_User_MaxID();
        }

        public bool DeleteUser(Int64 pID)
        {
            return dal_User.DeleteUser(pID);
        }

        public DataTable Get_User_ByUserPass(string pUser, string pPass)
        {
            return dal_User.Get_User_ByUserPass(pUser, pPass);
        }

        public bool IsExistUser(string pUser, string pPass)
        {
            if (Get_User_ByUserPass(pUser, pPass).Rows.Count > 0)
            {
                return true;
            }

            return false;
        }

        public List<User> Get_Nick_Support()
        {
            DataTable dt         = dal_User.Get_User_Dynamic("ISNULL(EMAIL,'0') <> '' OR ISNULL(SKYPE,'0') <> ''");
            List<User> lstReturn = Lst_User(dt);

            for (int i = 0; i < lstReturn.Count; i++)
            {
                string strEmail = lstReturn[i].EMAIL;
                if (strEmail.ToUpper().IndexOf("@YAHOO") > 0)
                {
                    lstReturn[i].EMAIL = strEmail.Substring(0, strEmail.ToUpper().IndexOf("@YAHOO"));
                }
                lstReturn[i].ID = i+1;
            }

            return lstReturn;
        }
    }
}
